<?php

namespace apiend\modules\user\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\helpers\Url;
use common\models\course\CourseGroup;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class GroupSearch extends \common\models\course\CourseGroupUsers {

    /**
     * 浏览历史
     * @param array $uid 检索参数
     * @return ActiveDataProvider
     */
    public function search($uid) {
        $query = static::find()
        ->alias('t')
        ->joinWith('group g')
        ->andWhere(['t.user_id' => $uid])
        ->andWhere(['g.status' => CourseGroup::STATUS_ACTIVE]);
        $query->orderBy([
            't.updated_at' => SORT_DESC,
        ]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query
        ]);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id' => 'group_id',
            'name' => function () {
                return $this->group->name;
            },
            'logo' => function () {
                return Url::toRemote($this->group->logo);
            },
            'nums' => function () {
                return $this->group->nums;
            },
        ];
    }

}
